package nju.bean.meeting.data;

import java.util.Timer;
import java.util.TimerTask;

public class Counter{

	private boolean redoToken;
	private boolean undoToken;
	private String redoUser;
	private String undoUser;
	private Timer redoTime;
	private Timer undoTime;
	
	
	public Counter(){
		redoToken = true;
		undoToken = true;
		redoUser = null;
		undoUser = null;
		redoTime = new Timer();
		undoTime = new Timer();
	}
	
	public void redo(String name){
		redoUser = name;
		this.redoToken = false;
		redoTime.cancel();
		redoTime = new Timer();
		redoTime.schedule(new MyTask(redoToken), 1000);
	}
	
	public void undo(String name){
		undoUser = name;
		this.undoToken = false;
		undoTime.cancel();
		undoTime = new Timer();
		undoTime.schedule(new MyTask(undoToken), 1000);
	}
	
	public boolean getRedoToken(String name){
		if(name.equals(redoUser))
			return true;
		return redoToken;
	}
	
	public boolean getUndoToken(String name){
		if(name.equals(undoUser))
			return true;
		return undoToken;
	}
	
	class MyTask extends TimerTask{

		private boolean ob;
		
		MyTask(boolean ob){
			this.ob = ob;
		}
			
		@Override
		public void run() {
			ob = true;
		}
		
	}
}
